package com.burgeon.backend.Rest.store;
/**
 * 按条件获取用户信息
 */


import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.Store;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindStoreData extends RESTBase {
    private final static String RANGE = "50";//分页数量

    private String ip;
    private String account;
    private String password;

    public FindStoreData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public HashMap getData(int start, String message) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(start, message), url);
        System.err.println(str);
        HashMap rows = new HashMap();

        String count = new JSONArray(str).getJSONObject(0).getString("count");

        rows.put("count", count);
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Store> list = new ArrayList<Store>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            Store store = new Store();
            store.setId(array.getString(0));
            store.setName(array.getString(1));
            store.setCustomerup(array.getString(2));
            store.setStorekind(array.getString(3));
            store.setArea(array.getString(4));
            store.setContactor(array.getString(5));
            store.setPhone(array.getString(6));
            store.setAddress(array.getString(7));
            store.setCreationdate((array.getString(8)).substring(0, 10));
            list.add(store);
        }

        rows.put("data", list);
        return rows;
    }

    private MultiValueMap Data(int y, String message) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");
        columns.add("NAME");//店名
        columns.add("'C_CUSTOMER_ID;NAME'");//经销商
        columns.add("'C_STOREKIND_ID;NAME'");//店仓性质
        columns.add("'C_AREA_ID;NAME'");//销售区域
        columns.add("CONTACTOR");//联系人
        columns.add("PHONE");//电话
        columns.add("ADDRESS");//地址
        columns.add("CREATIONDATE");//创建时间

        params.put("columns", columns);
        params.put("table", "C_STORE");//表名

        params.put("range", RANGE);
        params.put("count", "true");
        int r = Integer.valueOf(RANGE);
        params.put("start", String.valueOf(y * r));
        HashMap orderby = new HashMap();

        orderby.put("column", "CREATIONDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();
        String mage = "'" + message + "'";

        expr1.put("combine", "or");

        expr1v1.put("column", "'C_STOREKIND_ID;NAME'");//店铺性质
        expr1v1.put("condition", mage);
        expr1.put("expr1", expr1v1);

        expr1v2.put("column", "'C_AREA_ID;NAME'");//销售区域
        expr1v2.put("condition", mage);
        expr1.put("expr2", expr1v2);

        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        expr2.put("combine", "or");

        expr2v1.put("column", "'NAME'");//店仓
        expr2v1.put("condition", mage);
        expr2.put("expr1", expr2v1);

        expr2v2.put("column", "'C_CUSTOMER_ID;NAME'");//经销商
        expr2v2.put("condition", mage);
        expr2.put("expr2", expr2v2);

        paramss.put("combine", "or");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);


        params.put("params", paramss);
        return DATA("Query", params, account, password);
    }


    public static void main(String[] args) {
        FindStoreData g = new FindStoreData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData(0, "石家庄勒泰AVVN"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
